function test() {
  testXPCom();
}

function cleanDownloadBinningPref() {
  var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  pref.deleteBranch("extensions.downloadbinning");
  return;
}

function testXPCom() {

  try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    
    
    var main = Components.classes['@downloadbinning.net/main;1']
                                .getService(Components.interfaces.nsIDownloadBinning);

    var rules, output = "";

    rules = main.getRules({});
    output += "Initial rules: " + rules.length;
    output += "\nDelete all prefs";
    
    var mode = main.getDefaultMode();
    output += "\nDefaultMode: " + mode;
    cleanDownloadBinningPref();

    output += "\nChange to advanced mode";
    main.setDefaultMode(1); // Change default mode to Advanced mode
    output += "\nCurrent mode: " + main.getDefaultMode();
    
    var new_rule;
    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
    new_rule.setName("Word Document");
    new_rule.setDescription("Word Document.");
    new_rule.setDomain("", false);
    new_rule.setFilename("*.doc", false);
    new_rule.setDestination("D:\\My Documents");
    new_rule.setEnabled(true);
    var idx = main.addRule(new_rule);
    if (idx < 0) alert("Error while adding a new rule");

    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
    new_rule.setName("MP3");
    new_rule.setDescription("MP3");
    new_rule.setDomain("", false);
    new_rule.setFilename("*.mp3", false);
    new_rule.setDestination("D:\\MP3");
    new_rule.setEnabled(true);
    var idx = main.addRule(new_rule);
    if (idx < 0) alert("Error while adding a new rule");

    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
    new_rule.setName("Music files");
    new_rule.setDescription("All music files");
    new_rule.setDomain("", false);
    new_rule.setFilename("(\\.mp[34]\\.wma)$", true);
    new_rule.setDestination("D:\\MP3");
    new_rule.setEnabled(true);
    var idx = main.addRule(new_rule);
    if (idx < 0) alert("Error while adding a new rule");

    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
    new_rule.setName("Powerpoint");
    new_rule.setDescription("Powerpoint");
    new_rule.setDomain("", false);
    new_rule.setFilename("*.ppt", false);
    new_rule.setDestination("D:\\My Documents");
    new_rule.setEnabled(true);
    var idx = main.addRule(new_rule);
    if (idx < 0) alert("Error while adding a new rule");

    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
    new_rule.setName("Compressed file");
    new_rule.setDescription("bz2");
    new_rule.setDomain("", false);
    new_rule.setFilename("*.bz2", false);
    new_rule.setDestination("D:\\My Documents");
    new_rule.setEnabled(true);
    var idx = main.addRule(new_rule);
    if (idx < 0) alert("Error while adding a new rule");

    new_rule = Components.classes["@downloadbinning.net/rule;1"].createInstance(Components.interfaces.nsIDownloadBinningRule);
    new_rule.setName("Torrent");
    new_rule.setDescription("Torrent files");
    new_rule.setDomain("^http.*", true);
    new_rule.setFilename("\\.torrent$", true);
    new_rule.setDestination("D:\\Download\\Torrent Files");
    new_rule.setEnabled(true);
    var idx = main.addRule(new_rule);
    if (idx < 0) alert("Error while adding a new rule");
    output += "\n[getDomain] " + new_rule.getDomain();
    output += "\n[getFilename] " + new_rule.getFilename();    
    output += showRules(main.getRules({}), true);

    // MOVE DOWN
    output += "\n\nMoving item with index 2 up";
    if (!main.moveUp(2)) output += "Failed to move up";
    output += showRules(main.getRules({}), true);
    
    // MOVE UP
    output += "\n\nMoving item with index 2 down";
    if (!main.moveDown(2)) output += "Failed to move down";
    output += showRules(main.getRules({}), true);
    
    // REMOVE
    output += "Removing rule 1";
    main.removeRule(1);
    output += showRules(main.getRules({}), true);
    
    var r1 = main.getRule(1);
    output += "\n\nChanging Rule 1 (name = " + r1.getName() + ") to foobar";
    r1.setName("foobar");
    
    if (main.updateRule(1, r1)) {
      output += showRules(main.getRules({}), true);
    }
    
    var enabled_rules = main.getEnabledRules({});
    output += showRules(enabled_rules, true);
    
    alert(output);

  } catch (anError) {
    dump("ERROR: " + anError);
    alert("ERROR: " + anError);
  }
}

function showRules(rules, display_rulename) {
  output = "\n#Rules = " + rules.length + "\n";
  if (display_rulename) {
    for (var i = 0; i < rules.length; i++) {
      var r = rules[i];
      output += "Rule " + i + ": " + r.getName() + " (dRegEx: " + r.isDomainRegEx() + ", fRegEx: " + r.isFilenameRegEx() + ")\n";
    }
  }
  return output;
}

